﻿using System.ComponentModel;
using System.Reflection;
namespace Abp
{
    /// <summary>
    /// 实体注释获取帮助类
    /// </summary>
    public static class EntityDescriptionUtils
    {
        /// <summary>
        /// 获取实体注释
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTableDescription<T>()
        {
            var type = typeof(T);
            if (type.IsDefined(typeof(DescriptionAttribute), true))
                return type.GetCustomAttribute<DescriptionAttribute>().Description;
            return type.Name;
        }
        /// <summary>
        /// 获取属性注释
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetColumnDescription<T>(string name)
        {
            var type = typeof(T);
            var property = type.GetProperty(name, BindingFlags.Public |BindingFlags.Instance | BindingFlags.IgnoreCase);
            if (property.IsDefined(typeof(DescriptionAttribute), true))
                return property.GetCustomAttribute<DescriptionAttribute>().Description;
            return property.Name;
        }
    }
}
